import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
//调用示例：

//写入票据：that.$store.commit('loginauthor',author) 
//获取票据：新打开页面时会自动从Session中提取：
//that.$store.getters.getState;
//that.$store.getters.getState.author
//getter: that.$store.getters.getF2bpmSysConfig
//只有确定已登录时才能使用， that.$store.state.author.userId
const store = new Vuex.Store({
	state: {
		// 是否需要强制登录
		forcedLogin: true,
		//是否已登录
		hasLogin: false,
		//后台返回完整的autor,包括userId,orgId，orgName需要Url解码,realName需要Url解码,tenantId,token
		author: null,
		//接入的客户端校验应用ID
		corpId: "App190506100503",
		username: "" ,//用户名
		orgname: "" ,//部门科室

	},
	// 设置传值this.$store.commit('f2bpmSysConfig',f2bpmSysConfig) 的方式调用，实现充分解耦
	mutations: {
		//set方法
		f2bpmSysConfig(state, f2bpmSysConfig) {
			state.f2bpmSysConfig = f2bpmSysConfig;
			var f2bpmSysConfigStr = JSON.stringify(f2bpmSysConfig);
			sessionStorage.setItem('f2bpmSysConfig', f2bpmSysConfigStr);
		},
		loginauthor(state, author) {
			state.hasLogin = true;
			state.author = author;
			state.token = author.token;
			var authorStr = JSON.stringify(author);
			sessionStorage.setItem("f2bpmAuthor", authorStr);
		},
		user(state, data) {
			state.username = data.userName
			state.orgname = data.orgname
		},
		logout(state) {
			state.hasLogin = false;
			state.token = null;
			state.author = null;
			sessionStorage.removeItem("f2bpmAuthor");
			sessionStorage.removeItem("f2bpmSysConfig");
		}

	},
	//这里是get方法
	getters: {
		//获取store值 this.$store.getters.getF2bpmSysConfig
		//获取系统基础信息 
		getF2bpmSysConfig(state) {
			var f2bpmSysConfig = sessionStorage.getItem("f2bpmSysConfig");
			//如果没有，则去后台取
			if (state.hasLogin==false&&f2bpmSysConfig==null) {
				return null;
			}
			return eval("(" + f2bpmSysConfig + ")");
		},
		getState(state) {
			if (state.hasLogin) {
				return state;
			} else {
				var f2bpmAuthorStr = sessionStorage.getItem("f2bpmAuthor");
				if (f2bpmAuthorStr) {
					var f2bpmAuthor = eval("(" + f2bpmAuthorStr + ")");
					state.author = f2bpmAuthor;
					state.hasLogin = true;
				}
			}
			return state;
		},
		validateLogin(state) {
			if (state.hasLogin) {
				return true;
			} else {
				var f2bpmAuthorStr = sessionStorage.getItem("f2bpmAuthor");
				//===================尝试检测Url传递token登录======================
				//本地对传入Token的存储
				const accessTokenKey = 'accessToken'
				var local_accessToken = sessionStorage.getItem(accessTokenKey);
				var access_token = Object.toQueryString("token") || "";
				var isTokenLogin = false;
				if (access_token && !f2bpmAuthorStr) {
					console.info("首次token登录:" + access_token);
					isTokenLogin = true;
				}
				//校验本地的token是否与传入的一致，不一致时需要重新登录
				if (f2bpmAuthorStr != null && access_token && local_accessToken != access_token) {
					console.info("token重新登录:" + access_token);
					isTokenLogin = true;
				}
				if (isTokenLogin) {
					var parms = {
						"token": access_token
					};
					var result = Object.toAjaxJson("/login/", "loginByToken", parms, false);
					if (result.success) {
						var authorStr = JSON.stringify(result.data);
						f2bpmAuthorStr=authorStr;
						sessionStorage.setItem("f2bpmAuthor", authorStr);
						sessionStorage.setItem(accessTokenKey, access_token);
					} else {
						sessionStorage.removeItem("f2bpmAuthor");
						sessionStorage.removeItem(accessTokenKey);
						FUI.Window.showMsg("接入失败，请联运维管理员;" + result.msg);
					}
				}
				//==============================================================
	 
				//======如果是手动刷新浏览器时，需要尝试重新对state赋值=============
				if (f2bpmAuthorStr) {
					var f2bpmAuthor = eval("(" + f2bpmAuthorStr + ")");
					state.author = f2bpmAuthor;
					state.hasLogin = true;
				}
				//=============================================================
			}
			return state.hasLogin;
		}
	}
})
export default store
